<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>[elasticsearch]Elasticsearch在linux安装及使用 - Never Give Up</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="ZhangKQ" /><meta name="description" content="#Elasticsearch在linux安装及使用 一.Linux安装Elasticsearch 1.环境 php 7.2 elasticsearch 6.2.4 elasticsearch-php 6 2.安装JDK 网上教程很多" /><meta name="keywords" content="中间件, middleware, elasticsearch" />






<meta name="generator" content="Hugo 0.92.0 with theme even" />


<link rel="canonical" href="https://blog.nevergiveup.tech/post/middleware/elasticsearch%E5%9C%A8linux%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.b5a744db6de49a86cadafb3b70f555ab443f83c307a483402259e94726b045ff.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="[elasticsearch]Elasticsearch在linux安装及使用" />
<meta property="og:description" content="#Elasticsearch在linux安装及使用 一.Linux安装Elasticsearch 1.环境 php 7.2 elasticsearch 6.2.4 elasticsearch-php 6 2.安装JDK 网上教程很多" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://blog.nevergiveup.tech/post/middleware/elasticsearch%E5%9C%A8linux%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2018-05-13T16:37:56+08:00" />
<meta property="article:modified_time" content="2022-02-10T16:37:56+08:00" />

<meta itemprop="name" content="[elasticsearch]Elasticsearch在linux安装及使用">
<meta itemprop="description" content="#Elasticsearch在linux安装及使用 一.Linux安装Elasticsearch 1.环境 php 7.2 elasticsearch 6.2.4 elasticsearch-php 6 2.安装JDK 网上教程很多"><meta itemprop="datePublished" content="2018-05-13T16:37:56+08:00" />
<meta itemprop="dateModified" content="2022-02-10T16:37:56+08:00" />
<meta itemprop="wordCount" content="3132">
<meta itemprop="keywords" content="中间件,middleware,elasticsearch," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="[elasticsearch]Elasticsearch在linux安装及使用"/>
<meta name="twitter:description" content="#Elasticsearch在linux安装及使用 一.Linux安装Elasticsearch 1.环境 php 7.2 elasticsearch 6.2.4 elasticsearch-php 6 2.安装JDK 网上教程很多"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Never Give Up</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">主页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/remark/">
        <li class="mobile-menu-item">随言碎语</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">关于</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Never Give Up</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">主页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/remark/">随言碎语</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">关于</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">[elasticsearch]Elasticsearch在linux安装及使用</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-05-13 </span>
        <div class="post-category">
            <a href="/categories/%E4%B8%AD%E9%97%B4%E4%BB%B6/"> 中间件 </a>
            </div>
          <span class="more-meta"> 约 3132 字 </span>
          <span class="more-meta"> 预计阅读 7 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#一linux安装elasticsearch"><strong>一.Linux安装Elasticsearch</strong></a>
          <ul>
            <li><a href="#1环境"><strong>1.环境</strong></a></li>
            <li><a href="#2安装jdk"><strong>2.安装JDK</strong></a></li>
            <li><a href="#3安装elasticsearch"><strong>3.安装Elasticsearch</strong></a></li>
            <li><a href="#4安装elasticsearch遇到的问题"><strong>4.安装Elasticsearch遇到的问题</strong></a></li>
            <li><a href="#5访问api"><strong>5.访问API</strong></a></li>
            <li><a href="#6可视化工具dejavu安装"><strong>6.可视化工具Dejavu安装</strong></a></li>
          </ul>
        </li>
        <li><a href="#二项目安装elasticsearch-php"><strong>二.项目安装Elasticsearch-PHP</strong></a>
          <ul>
            <li><a href="#1php项目安装扩展包"><strong>1.php项目安装扩展包</strong></a></li>
            <li><a href="#2代码示例"><strong>2.代码示例</strong></a></li>
            <li><a href="#3保持-elasticsearch数据最新"><strong>3.保持</strong> <strong>Elasticsearch数据最新</strong></a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>#Elasticsearch在linux安装及使用</p>
<h2 id="一linux安装elasticsearch"><strong>一.Linux安装Elasticsearch</strong></h2>
<h3 id="1环境"><strong>1.环境</strong></h3>
<p>php 7.2</p>
<p>elasticsearch 6.2.4</p>
<p>elasticsearch-php 6</p>
<h3 id="2安装jdk"><strong>2.安装JDK</strong></h3>
<p>网上教程很多，也可以参考本人教程之前写的<a href="https://www.jianshu.com/p/4418cb3bda31">Linux安装JDK</a></p>
<h3 id="3安装elasticsearch"><strong>3.安装Elasticsearch</strong></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="nb">cd</span> /data/LPAPP

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz

tar zxvf elasticsearch-6.2.4.tar.gz

useradd es

passwd es

chown es:eselasticsearch-6.2.4

<span class="nb">cd</span> elasticsearch-6.2.4

vim ./config/elasticsearch.yml

添加 network.host: 0.0.0.0

su es

./bin/elasticsearch  // 启动

报错需重新进入es用户

<span class="nb">exit</span>

su es
</code></pre></td></tr></table>
</div>
</div><h3 id="4安装elasticsearch遇到的问题"><strong>4.安装Elasticsearch遇到的问题</strong></h3>
<h4 id="1max-virtual-memory-areas-vmmax_map_count-65530-is-too-low-increase-to-at-least">1).max virtual memory areas vm.max_map_count [65530] is too low, increase to at least</h4>
<p>root用户下</p>
<p>vim /etc/sysctl.conf</p>
<p>vm.max_map_count = 655360</p>
<p>/sbin/sysctl -p</p>
<h4 id="2max-number-of-threads-3818-for-user-es-is-too-low-increase-to-at-least-4096">2).max number of threads [3818] for user [es] is too low, increase to at least [4096]</h4>
<p>root用户下</p>
<p>vim etc/security/limits.conf</p>
<p>* soft nproc 4096</p>
<p>* hard nproc 4096</p>
<h4 id="3max-file-descriptors-4096-for-elasticsearch-process-is-too-low-increase-to-at-least-65536">3).max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]</h4>
<p>root用户下</p>
<p>vim /etc/security/limits.conf</p>
<p>* soft nofile 65536</p>
<p>* hard nofile 65536</p>
<h4 id="4system-call-filters-failed-to-install-check-the-logs-and-fix-your-configuration-or-disable-system-call-filters-at-your-own-risk">4).system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk</h4>
<p>在elasticsearch.yml中配置bootstrap.system_call_filter为false</p>
<p>bootstrap.memory_lock: false</p>
<p>bootstrap.system_call_filter: false</p>
<h3 id="5访问api"><strong>5.访问API</strong></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">curl -X GET http://127.0.0.1:9200

或

http://192.168.20.131:9200

<span class="o">{</span>

  <span class="s2">&#34;name&#34;</span> : <span class="s2">&#34;Hw6FKjp&#34;</span>,

  <span class="s2">&#34;cluster_name&#34;</span> : <span class="s2">&#34;elasticsearch&#34;</span>,

  <span class="s2">&#34;cluster_uuid&#34;</span> : <span class="s2">&#34;V4QjQP5JT16hjpkohQ1fpg&#34;</span>,

  <span class="s2">&#34;version&#34;</span> : <span class="o">{</span>

    <span class="s2">&#34;number&#34;</span> : <span class="s2">&#34;6.2.4&#34;</span>,

    <span class="s2">&#34;build_hash&#34;</span> : <span class="s2">&#34;ccec39f&#34;</span>,

    <span class="s2">&#34;build_date&#34;</span> : <span class="s2">&#34;2018-04-12T20:37:28.497551Z&#34;</span>,

    <span class="s2">&#34;build_snapshot&#34;</span> : false,

    <span class="s2">&#34;lucene_version&#34;</span> : <span class="s2">&#34;7.2.1&#34;</span>,

    <span class="s2">&#34;minimum_wire_compatibility_version&#34;</span> : <span class="s2">&#34;5.6.0&#34;</span>,

    <span class="s2">&#34;minimum_index_compatibility_version&#34;</span> : <span class="s2">&#34;5.0.0&#34;</span>

  <span class="o">}</span>,

  <span class="s2">&#34;tagline&#34;</span> : <span class="s2">&#34;You Know, for Search&#34;</span>

<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="6可视化工具dejavu安装"><strong>6.可视化工具Dejavu安装</strong></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">$ docker run -p 1358:1358 -d appbaseio/dejavu


open http://localhost:1358/
</code></pre></td></tr></table>
</div>
</div><h2 id="二项目安装elasticsearch-php"><strong>二.项目安装Elasticsearch-PHP</strong></h2>
<h3 id="1php项目安装扩展包"><strong>1.php项目安装扩展包</strong></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-php" data-lang="php"><span class="nx">composer</span><span class="o">.</span><span class="nx">json</span> <span class="nx">文件中加入</span> <span class="s2">&#34;elasticsearch/elasticsearch&#34;</span><span class="o">:</span> <span class="s2">&#34;~6.0&#34;</span>

<span class="nx">composer</span> <span class="nx">update</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="2代码示例"><strong>2.代码示例</strong></h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span><span class="lnt">109
</span><span class="lnt">110
</span><span class="lnt">111
</span><span class="lnt">112
</span><span class="lnt">113
</span><span class="lnt">114
</span><span class="lnt">115
</span><span class="lnt">116
</span><span class="lnt">117
</span><span class="lnt">118
</span><span class="lnt">119
</span><span class="lnt">120
</span><span class="lnt">121
</span><span class="lnt">122
</span><span class="lnt">123
</span><span class="lnt">124
</span><span class="lnt">125
</span><span class="lnt">126
</span><span class="lnt">127
</span><span class="lnt">128
</span><span class="lnt">129
</span><span class="lnt">130
</span><span class="lnt">131
</span><span class="lnt">132
</span><span class="lnt">133
</span><span class="lnt">134
</span><span class="lnt">135
</span><span class="lnt">136
</span><span class="lnt">137
</span><span class="lnt">138
</span><span class="lnt">139
</span><span class="lnt">140
</span><span class="lnt">141
</span><span class="lnt">142
</span><span class="lnt">143
</span><span class="lnt">144
</span><span class="lnt">145
</span><span class="lnt">146
</span><span class="lnt">147
</span><span class="lnt">148
</span><span class="lnt">149
</span><span class="lnt">150
</span><span class="lnt">151
</span><span class="lnt">152
</span><span class="lnt">153
</span><span class="lnt">154
</span><span class="lnt">155
</span><span class="lnt">156
</span><span class="lnt">157
</span><span class="lnt">158
</span><span class="lnt">159
</span><span class="lnt">160
</span><span class="lnt">161
</span><span class="lnt">162
</span><span class="lnt">163
</span><span class="lnt">164
</span><span class="lnt">165
</span><span class="lnt">166
</span><span class="lnt">167
</span><span class="lnt">168
</span><span class="lnt">169
</span><span class="lnt">170
</span><span class="lnt">171
</span><span class="lnt">172
</span><span class="lnt">173
</span><span class="lnt">174
</span><span class="lnt">175
</span><span class="lnt">176
</span><span class="lnt">177
</span><span class="lnt">178
</span><span class="lnt">179
</span><span class="lnt">180
</span><span class="lnt">181
</span><span class="lnt">182
</span><span class="lnt">183
</span><span class="lnt">184
</span><span class="lnt">185
</span><span class="lnt">186
</span><span class="lnt">187
</span><span class="lnt">188
</span><span class="lnt">189
</span><span class="lnt">190
</span><span class="lnt">191
</span><span class="lnt">192
</span><span class="lnt">193
</span><span class="lnt">194
</span><span class="lnt">195
</span><span class="lnt">196
</span><span class="lnt">197
</span><span class="lnt">198
</span><span class="lnt">199
</span><span class="lnt">200
</span><span class="lnt">201
</span><span class="lnt">202
</span><span class="lnt">203
</span><span class="lnt">204
</span><span class="lnt">205
</span><span class="lnt">206
</span><span class="lnt">207
</span><span class="lnt">208
</span><span class="lnt">209
</span><span class="lnt">210
</span><span class="lnt">211
</span><span class="lnt">212
</span><span class="lnt">213
</span><span class="lnt">214
</span><span class="lnt">215
</span><span class="lnt">216
</span><span class="lnt">217
</span><span class="lnt">218
</span><span class="lnt">219
</span><span class="lnt">220
</span><span class="lnt">221
</span><span class="lnt">222
</span><span class="lnt">223
</span><span class="lnt">224
</span><span class="lnt">225
</span><span class="lnt">226
</span><span class="lnt">227
</span><span class="lnt">228
</span><span class="lnt">229
</span><span class="lnt">230
</span><span class="lnt">231
</span><span class="lnt">232
</span><span class="lnt">233
</span><span class="lnt">234
</span><span class="lnt">235
</span><span class="lnt">236
</span><span class="lnt">237
</span><span class="lnt">238
</span><span class="lnt">239
</span><span class="lnt">240
</span><span class="lnt">241
</span><span class="lnt">242
</span><span class="lnt">243
</span><span class="lnt">244
</span><span class="lnt">245
</span><span class="lnt">246
</span><span class="lnt">247
</span><span class="lnt">248
</span><span class="lnt">249
</span><span class="lnt">250
</span><span class="lnt">251
</span><span class="lnt">252
</span><span class="lnt">253
</span><span class="lnt">254
</span><span class="lnt">255
</span><span class="lnt">256
</span><span class="lnt">257
</span><span class="lnt">258
</span><span class="lnt">259
</span><span class="lnt">260
</span><span class="lnt">261
</span><span class="lnt">262
</span><span class="lnt">263
</span><span class="lnt">264
</span><span class="lnt">265
</span><span class="lnt">266
</span><span class="lnt">267
</span><span class="lnt">268
</span><span class="lnt">269
</span><span class="lnt">270
</span><span class="lnt">271
</span><span class="lnt">272
</span><span class="lnt">273
</span><span class="lnt">274
</span><span class="lnt">275
</span><span class="lnt">276
</span><span class="lnt">277
</span><span class="lnt">278
</span><span class="lnt">279
</span><span class="lnt">280
</span><span class="lnt">281
</span><span class="lnt">282
</span><span class="lnt">283
</span><span class="lnt">284
</span><span class="lnt">285
</span><span class="lnt">286
</span><span class="lnt">287
</span><span class="lnt">288
</span><span class="lnt">289
</span><span class="lnt">290
</span><span class="lnt">291
</span><span class="lnt">292
</span><span class="lnt">293
</span><span class="lnt">294
</span><span class="lnt">295
</span><span class="lnt">296
</span><span class="lnt">297
</span><span class="lnt">298
</span><span class="lnt">299
</span><span class="lnt">300
</span><span class="lnt">301
</span><span class="lnt">302
</span><span class="lnt">303
</span><span class="lnt">304
</span><span class="lnt">305
</span><span class="lnt">306
</span><span class="lnt">307
</span><span class="lnt">308
</span><span class="lnt">309
</span><span class="lnt">310
</span><span class="lnt">311
</span><span class="lnt">312
</span><span class="lnt">313
</span><span class="lnt">314
</span><span class="lnt">315
</span><span class="lnt">316
</span><span class="lnt">317
</span><span class="lnt">318
</span><span class="lnt">319
</span><span class="lnt">320
</span><span class="lnt">321
</span><span class="lnt">322
</span><span class="lnt">323
</span><span class="lnt">324
</span><span class="lnt">325
</span><span class="lnt">326
</span><span class="lnt">327
</span><span class="lnt">328
</span><span class="lnt">329
</span><span class="lnt">330
</span><span class="lnt">331
</span><span class="lnt">332
</span><span class="lnt">333
</span><span class="lnt">334
</span><span class="lnt">335
</span><span class="lnt">336
</span><span class="lnt">337
</span><span class="lnt">338
</span><span class="lnt">339
</span><span class="lnt">340
</span><span class="lnt">341
</span><span class="lnt">342
</span><span class="lnt">343
</span><span class="lnt">344
</span><span class="lnt">345
</span><span class="lnt">346
</span><span class="lnt">347
</span><span class="lnt">348
</span><span class="lnt">349
</span><span class="lnt">350
</span><span class="lnt">351
</span><span class="lnt">352
</span><span class="lnt">353
</span><span class="lnt">354
</span><span class="lnt">355
</span><span class="lnt">356
</span><span class="lnt">357
</span><span class="lnt">358
</span><span class="lnt">359
</span><span class="lnt">360
</span><span class="lnt">361
</span><span class="lnt">362
</span><span class="lnt">363
</span><span class="lnt">364
</span><span class="lnt">365
</span><span class="lnt">366
</span><span class="lnt">367
</span><span class="lnt">368
</span><span class="lnt">369
</span><span class="lnt">370
</span><span class="lnt">371
</span><span class="lnt">372
</span><span class="lnt">373
</span><span class="lnt">374
</span><span class="lnt">375
</span><span class="lnt">376
</span><span class="lnt">377
</span><span class="lnt">378
</span><span class="lnt">379
</span><span class="lnt">380
</span><span class="lnt">381
</span><span class="lnt">382
</span><span class="lnt">383
</span><span class="lnt">384
</span><span class="lnt">385
</span><span class="lnt">386
</span><span class="lnt">387
</span><span class="lnt">388
</span><span class="lnt">389
</span><span class="lnt">390
</span><span class="lnt">391
</span><span class="lnt">392
</span><span class="lnt">393
</span><span class="lnt">394
</span><span class="lnt">395
</span><span class="lnt">396
</span><span class="lnt">397
</span><span class="lnt">398
</span><span class="lnt">399
</span><span class="lnt">400
</span><span class="lnt">401
</span><span class="lnt">402
</span><span class="lnt">403
</span><span class="lnt">404
</span><span class="lnt">405
</span><span class="lnt">406
</span><span class="lnt">407
</span><span class="lnt">408
</span><span class="lnt">409
</span><span class="lnt">410
</span><span class="lnt">411
</span><span class="lnt">412
</span><span class="lnt">413
</span><span class="lnt">414
</span><span class="lnt">415
</span><span class="lnt">416
</span><span class="lnt">417
</span><span class="lnt">418
</span><span class="lnt">419
</span><span class="lnt">420
</span><span class="lnt">421
</span><span class="lnt">422
</span><span class="lnt">423
</span><span class="lnt">424
</span><span class="lnt">425
</span><span class="lnt">426
</span><span class="lnt">427
</span><span class="lnt">428
</span><span class="lnt">429
</span><span class="lnt">430
</span><span class="lnt">431
</span><span class="lnt">432
</span><span class="lnt">433
</span><span class="lnt">434
</span><span class="lnt">435
</span><span class="lnt">436
</span><span class="lnt">437
</span><span class="lnt">438
</span><span class="lnt">439
</span><span class="lnt">440
</span><span class="lnt">441
</span><span class="lnt">442
</span><span class="lnt">443
</span><span class="lnt">444
</span><span class="lnt">445
</span><span class="lnt">446
</span><span class="lnt">447
</span><span class="lnt">448
</span><span class="lnt">449
</span><span class="lnt">450
</span><span class="lnt">451
</span><span class="lnt">452
</span><span class="lnt">453
</span><span class="lnt">454
</span><span class="lnt">455
</span><span class="lnt">456
</span><span class="lnt">457
</span><span class="lnt">458
</span><span class="lnt">459
</span><span class="lnt">460
</span><span class="lnt">461
</span><span class="lnt">462
</span><span class="lnt">463
</span><span class="lnt">464
</span><span class="lnt">465
</span><span class="lnt">466
</span><span class="lnt">467
</span><span class="lnt">468
</span><span class="lnt">469
</span><span class="lnt">470
</span><span class="lnt">471
</span><span class="lnt">472
</span><span class="lnt">473
</span><span class="lnt">474
</span><span class="lnt">475
</span><span class="lnt">476
</span><span class="lnt">477
</span><span class="lnt">478
</span><span class="lnt">479
</span><span class="lnt">480
</span><span class="lnt">481
</span><span class="lnt">482
</span><span class="lnt">483
</span><span class="lnt">484
</span><span class="lnt">485
</span><span class="lnt">486
</span><span class="lnt">487
</span><span class="lnt">488
</span><span class="lnt">489
</span><span class="lnt">490
</span><span class="lnt">491
</span><span class="lnt">492
</span><span class="lnt">493
</span><span class="lnt">494
</span><span class="lnt">495
</span><span class="lnt">496
</span><span class="lnt">497
</span><span class="lnt">498
</span><span class="lnt">499
</span><span class="lnt">500
</span><span class="lnt">501
</span><span class="lnt">502
</span><span class="lnt">503
</span><span class="lnt">504
</span><span class="lnt">505
</span><span class="lnt">506
</span><span class="lnt">507
</span><span class="lnt">508
</span><span class="lnt">509
</span><span class="lnt">510
</span><span class="lnt">511
</span><span class="lnt">512
</span><span class="lnt">513
</span><span class="lnt">514
</span><span class="lnt">515
</span><span class="lnt">516
</span><span class="lnt">517
</span><span class="lnt">518
</span><span class="lnt">519
</span><span class="lnt">520
</span><span class="lnt">521
</span><span class="lnt">522
</span><span class="lnt">523
</span><span class="lnt">524
</span><span class="lnt">525
</span><span class="lnt">526
</span><span class="lnt">527
</span><span class="lnt">528
</span><span class="lnt">529
</span><span class="lnt">530
</span><span class="lnt">531
</span><span class="lnt">532
</span><span class="lnt">533
</span><span class="lnt">534
</span><span class="lnt">535
</span><span class="lnt">536
</span><span class="lnt">537
</span><span class="lnt">538
</span><span class="lnt">539
</span><span class="lnt">540
</span><span class="lnt">541
</span><span class="lnt">542
</span><span class="lnt">543
</span><span class="lnt">544
</span><span class="lnt">545
</span><span class="lnt">546
</span><span class="lnt">547
</span><span class="lnt">548
</span><span class="lnt">549
</span><span class="lnt">550
</span><span class="lnt">551
</span><span class="lnt">552
</span><span class="lnt">553
</span><span class="lnt">554
</span><span class="lnt">555
</span><span class="lnt">556
</span><span class="lnt">557
</span><span class="lnt">558
</span><span class="lnt">559
</span><span class="lnt">560
</span><span class="lnt">561
</span><span class="lnt">562
</span><span class="lnt">563
</span><span class="lnt">564
</span><span class="lnt">565
</span><span class="lnt">566
</span><span class="lnt">567
</span><span class="lnt">568
</span><span class="lnt">569
</span><span class="lnt">570
</span><span class="lnt">571
</span><span class="lnt">572
</span><span class="lnt">573
</span><span class="lnt">574
</span><span class="lnt">575
</span><span class="lnt">576
</span><span class="lnt">577
</span><span class="lnt">578
</span><span class="lnt">579
</span><span class="lnt">580
</span><span class="lnt">581
</span><span class="lnt">582
</span><span class="lnt">583
</span><span class="lnt">584
</span><span class="lnt">585
</span><span class="lnt">586
</span><span class="lnt">587
</span><span class="lnt">588
</span><span class="lnt">589
</span><span class="lnt">590
</span><span class="lnt">591
</span><span class="lnt">592
</span><span class="lnt">593
</span><span class="lnt">594
</span><span class="lnt">595
</span><span class="lnt">596
</span><span class="lnt">597
</span><span class="lnt">598
</span><span class="lnt">599
</span><span class="lnt">600
</span><span class="lnt">601
</span><span class="lnt">602
</span><span class="lnt">603
</span><span class="lnt">604
</span><span class="lnt">605
</span><span class="lnt">606
</span><span class="lnt">607
</span><span class="lnt">608
</span><span class="lnt">609
</span><span class="lnt">610
</span><span class="lnt">611
</span><span class="lnt">612
</span><span class="lnt">613
</span><span class="lnt">614
</span><span class="lnt">615
</span><span class="lnt">616
</span><span class="lnt">617
</span><span class="lnt">618
</span><span class="lnt">619
</span><span class="lnt">620
</span><span class="lnt">621
</span><span class="lnt">622
</span><span class="lnt">623
</span><span class="lnt">624
</span><span class="lnt">625
</span><span class="lnt">626
</span><span class="lnt">627
</span><span class="lnt">628
</span><span class="lnt">629
</span><span class="lnt">630
</span><span class="lnt">631
</span><span class="lnt">632
</span><span class="lnt">633
</span><span class="lnt">634
</span><span class="lnt">635
</span><span class="lnt">636
</span><span class="lnt">637
</span><span class="lnt">638
</span><span class="lnt">639
</span><span class="lnt">640
</span><span class="lnt">641
</span><span class="lnt">642
</span><span class="lnt">643
</span><span class="lnt">644
</span><span class="lnt">645
</span><span class="lnt">646
</span><span class="lnt">647
</span><span class="lnt">648
</span><span class="lnt">649
</span><span class="lnt">650
</span><span class="lnt">651
</span><span class="lnt">652
</span><span class="lnt">653
</span><span class="lnt">654
</span><span class="lnt">655
</span><span class="lnt">656
</span><span class="lnt">657
</span><span class="lnt">658
</span><span class="lnt">659
</span><span class="lnt">660
</span><span class="lnt">661
</span><span class="lnt">662
</span><span class="lnt">663
</span><span class="lnt">664
</span><span class="lnt">665
</span><span class="lnt">666
</span><span class="lnt">667
</span><span class="lnt">668
</span><span class="lnt">669
</span><span class="lnt">670
</span><span class="lnt">671
</span><span class="lnt">672
</span><span class="lnt">673
</span><span class="lnt">674
</span><span class="lnt">675
</span><span class="lnt">676
</span><span class="lnt">677
</span><span class="lnt">678
</span><span class="lnt">679
</span><span class="lnt">680
</span><span class="lnt">681
</span><span class="lnt">682
</span><span class="lnt">683
</span><span class="lnt">684
</span><span class="lnt">685
</span><span class="lnt">686
</span><span class="lnt">687
</span><span class="lnt">688
</span><span class="lnt">689
</span><span class="lnt">690
</span><span class="lnt">691
</span><span class="lnt">692
</span><span class="lnt">693
</span><span class="lnt">694
</span><span class="lnt">695
</span><span class="lnt">696
</span><span class="lnt">697
</span><span class="lnt">698
</span><span class="lnt">699
</span><span class="lnt">700
</span><span class="lnt">701
</span><span class="lnt">702
</span><span class="lnt">703
</span><span class="lnt">704
</span><span class="lnt">705
</span><span class="lnt">706
</span><span class="lnt">707
</span><span class="lnt">708
</span><span class="lnt">709
</span><span class="lnt">710
</span><span class="lnt">711
</span><span class="lnt">712
</span><span class="lnt">713
</span><span class="lnt">714
</span><span class="lnt">715
</span><span class="lnt">716
</span><span class="lnt">717
</span><span class="lnt">718
</span><span class="lnt">719
</span><span class="lnt">720
</span><span class="lnt">721
</span><span class="lnt">722
</span><span class="lnt">723
</span><span class="lnt">724
</span><span class="lnt">725
</span><span class="lnt">726
</span><span class="lnt">727
</span><span class="lnt">728
</span><span class="lnt">729
</span><span class="lnt">730
</span><span class="lnt">731
</span><span class="lnt">732
</span><span class="lnt">733
</span><span class="lnt">734
</span><span class="lnt">735
</span><span class="lnt">736
</span><span class="lnt">737
</span><span class="lnt">738
</span><span class="lnt">739
</span><span class="lnt">740
</span><span class="lnt">741
</span><span class="lnt">742
</span><span class="lnt">743
</span><span class="lnt">744
</span><span class="lnt">745
</span><span class="lnt">746
</span><span class="lnt">747
</span><span class="lnt">748
</span><span class="lnt">749
</span><span class="lnt">750
</span><span class="lnt">751
</span><span class="lnt">752
</span><span class="lnt">753
</span><span class="lnt">754
</span><span class="lnt">755
</span><span class="lnt">756
</span><span class="lnt">757
</span><span class="lnt">758
</span><span class="lnt">759
</span><span class="lnt">760
</span><span class="lnt">761
</span><span class="lnt">762
</span><span class="lnt">763
</span><span class="lnt">764
</span><span class="lnt">765
</span><span class="lnt">766
</span><span class="lnt">767
</span><span class="lnt">768
</span><span class="lnt">769
</span><span class="lnt">770
</span><span class="lnt">771
</span><span class="lnt">772
</span><span class="lnt">773
</span><span class="lnt">774
</span><span class="lnt">775
</span><span class="lnt">776
</span><span class="lnt">777
</span><span class="lnt">778
</span><span class="lnt">779
</span><span class="lnt">780
</span><span class="lnt">781
</span><span class="lnt">782
</span><span class="lnt">783
</span><span class="lnt">784
</span><span class="lnt">785
</span><span class="lnt">786
</span><span class="lnt">787
</span><span class="lnt">788
</span><span class="lnt">789
</span><span class="lnt">790
</span><span class="lnt">791
</span><span class="lnt">792
</span><span class="lnt">793
</span><span class="lnt">794
</span><span class="lnt">795
</span><span class="lnt">796
</span><span class="lnt">797
</span><span class="lnt">798
</span><span class="lnt">799
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-php" data-lang="php"><span class="o">&lt;?</span><span class="nx">php</span>

 

 

<span class="k">namespace</span> <span class="nx">App\Http\Controllers\Test</span><span class="p">;</span>

 

 

<span class="k">use</span> <span class="nx">App\Handler\Traits\RedisTrait\ConfigCollegeTrait</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">App\Http\Controllers\Controller</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">App\Models\Course\CourseImproveLinkModel</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Elasticsearch\ClientBuilder</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">Illuminate\Http\Request</span><span class="p">;</span>

 

<span class="sd">/**
</span><span class="sd">
</span><span class="sd"> * es搜索用例
</span><span class="sd">
</span><span class="sd"> * Class ElasticSearchTestController
</span><span class="sd">
</span><span class="sd"> * @package App\Http\Controllers\Test
</span><span class="sd">
</span><span class="sd"> */</span>

<span class="k">class</span> <span class="nc">ElasticSearchTestController</span> <span class="k">extends</span> <span class="nx">Controller</span>

<span class="p">{</span>

    <span class="k">use</span> <span class="nx">ConfigCollegeTrait</span><span class="p">;</span>

 

    <span class="k">protected</span> <span class="nv">$client</span><span class="p">;</span>

 

    <span class="k">function</span> <span class="fm">__construct</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="c1">//从hash配置中创建客户端
</span><span class="c1"></span>
        <span class="nv">$params</span>       <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;hosts&#39;</span>   <span class="o">=&gt;</span> <span class="p">[</span>

                <span class="s1">&#39;192.168.20.138:9200&#39;</span><span class="c1">//host
</span><span class="c1"></span>
            <span class="p">],</span>

            <span class="s1">&#39;retries&#39;</span> <span class="o">=&gt;</span> <span class="mi">2</span><span class="p">,</span><span class="c1">//重连次数
</span><span class="c1"></span>
            <span class="s1">&#39;handler&#39;</span> <span class="o">=&gt;</span> <span class="nx">ClientBuilder</span><span class="o">::</span><span class="na">singleHandler</span><span class="p">()</span><span class="c1">//http handler模式
</span><span class="c1"></span>
        <span class="p">];</span>

        <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span> <span class="o">=</span> <span class="nx">ClientBuilder</span><span class="o">::</span><span class="na">fromConfig</span><span class="p">(</span><span class="nx">params</span><span class="p">);</span>

        <span class="c1">//$this-&gt;client = ClientBuilder::create()-&gt;build();
</span><span class="c1"></span>
    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 入口程序
</span><span class="sd">
</span><span class="sd">     * @param Request $request
</span><span class="sd">
</span><span class="sd">     * @return \Illuminate\Http\JsonResponse
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">(</span><span class="nx">Request</span> <span class="nv">$request</span><span class="p">)</span>

    <span class="p">{</span>

        <span class="c1">//请求类型:1索引一个文档,2删除一个文档,3获取一个文档,4搜索一个文档,5删除一个索引,6创建一个索引,7更新一个文档,8批量索引,9索引示例
</span><span class="c1"></span>
        <span class="nx">type</span> <span class="o">=</span> <span class="nx">request</span><span class="o">-&gt;</span><span class="na">input</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>

        <span class="c1">//9索引示例:1批量创建索引,2获取文档,3搜索文档,4删除索引
</span><span class="c1"></span>
        <span class="nx">exampleType</span> <span class="o">=</span> <span class="nx">request</span><span class="o">-&gt;</span><span class="na">input</span><span class="p">(</span><span class="s1">&#39;example_type&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>

        <span class="c1">//搜索关键字
</span><span class="c1"></span>
        <span class="nx">keywords</span> <span class="o">=</span> <span class="nx">request</span><span class="o">-&gt;</span><span class="na">input</span><span class="p">(</span><span class="s1">&#39;keywords&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">);</span>

        <span class="c1">//分页
</span><span class="c1"></span>
        <span class="nx">page</span>     <span class="o">=</span> <span class="nx">request</span><span class="o">-&gt;</span><span class="na">input</span><span class="p">(</span><span class="s1">&#39;page&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>

        <span class="nx">pageSize</span> <span class="o">=</span> <span class="nx">request</span><span class="o">-&gt;</span><span class="na">input</span><span class="p">(</span><span class="s1">&#39;page_size&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span>

 

        <span class="k">try</span> <span class="p">{</span>

            <span class="nv">$response</span> <span class="o">=</span> <span class="p">[];</span>

            <span class="k">switch</span> <span class="p">(</span><span class="nv">$type</span><span class="p">)</span> <span class="p">{</span>

                <span class="k">case</span> <span class="mi">1</span><span class="o">:</span><span class="c1">#1索引一个文档
</span><span class="c1"></span>
                    <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">addTextIndex</span><span class="p">();</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">case</span> <span class="mi">2</span><span class="o">:</span><span class="c1">#2删除一个文档
</span><span class="c1"></span>
                    <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">delTextIndex</span><span class="p">();</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">case</span> <span class="mi">3</span><span class="o">:</span><span class="c1">#3获取一个文档
</span><span class="c1"></span>
                    <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">getTextIndex</span><span class="p">();</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">case</span> <span class="mi">4</span><span class="o">:</span><span class="c1">#4搜索一个文档
</span><span class="c1"></span>
                    <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">searchText</span><span class="p">();</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">case</span> <span class="mi">5</span><span class="o">:</span><span class="c1">#5删除一个索引
</span><span class="c1"></span>
                    <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">delIndex</span><span class="p">();</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">case</span> <span class="mi">6</span><span class="o">:</span><span class="c1">#6创建一个索引
</span><span class="c1"></span>
                    <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">addIndex</span><span class="p">();</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">case</span> <span class="mi">7</span><span class="o">:</span><span class="c1">#7更新一个文档
</span><span class="c1"></span>
                    <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">updateTextIndex</span><span class="p">();</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">case</span> <span class="mi">8</span><span class="o">:</span><span class="c1">#8批量索引
</span><span class="c1"></span>
                    <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">bulkTextIndex</span><span class="p">();</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">case</span> <span class="mi">9</span><span class="o">:</span><span class="c1">#9索引示例
</span><span class="c1"></span>
                    <span class="k">switch</span> <span class="p">(</span><span class="nv">$exampleType</span><span class="p">)</span> <span class="p">{</span>

                        <span class="k">case</span> <span class="mi">1</span><span class="o">:</span><span class="c1">#1批量创建索引
</span><span class="c1"></span>
                            <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">bulkTextIndexExample</span><span class="p">();</span>

                            <span class="k">break</span><span class="p">;</span>

                        <span class="k">case</span> <span class="mi">2</span><span class="o">:</span><span class="c1">#2获取文档
</span><span class="c1"></span>
                            <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">getTextIndexExample</span><span class="p">();</span>

                            <span class="k">break</span><span class="p">;</span>

                        <span class="k">case</span> <span class="mi">3</span><span class="o">:</span><span class="c1">#3搜索文档
</span><span class="c1"></span>
                            <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">searchTextExample</span><span class="p">(</span><span class="nx">keywords</span><span class="p">,</span> <span class="nx">page</span><span class="p">,</span> <span class="nv">$pageSize</span><span class="p">);</span>

                            <span class="k">break</span><span class="p">;</span>

                        <span class="k">case</span> <span class="mi">4</span><span class="o">:</span><span class="c1">#4删除索引
</span><span class="c1"></span>
                            <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">delIndexExample</span><span class="p">();</span>

                            <span class="k">break</span><span class="p">;</span>

                    <span class="p">}</span>

                    <span class="k">break</span><span class="p">;</span>

                <span class="k">default</span><span class="o">:</span>

                    <span class="k">break</span><span class="p">;</span>

            <span class="p">}</span>

 

            <span class="k">return</span> <span class="nx">responseSuccess</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="nv">$response</span><span class="p">);</span>

        <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\Exception</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>

            <span class="nx">returnData</span><span class="p">[</span><span class="s1">&#39;code&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nx">e</span><span class="o">-&gt;</span><span class="na">getCode</span><span class="p">();</span>

            <span class="nx">returnData</span><span class="p">[</span><span class="s1">&#39;msg&#39;</span><span class="p">]</span>  <span class="o">=</span> <span class="nx">json_decode</span><span class="p">(</span><span class="nx">e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">(),</span> <span class="k">true</span><span class="p">);</span>

 

            <span class="k">return</span> <span class="nx">responseSuccess</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="nv">$returnData</span><span class="p">);</span>

        <span class="p">}</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 索引一个文档
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">addTextIndex</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_index&#39;</span><span class="p">,</span>

            <span class="s1">&#39;type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;my_type&#39;</span><span class="p">,</span>

            <span class="s1">&#39;id&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;my_id&#39;</span><span class="p">,</span>

            <span class="s1">&#39;body&#39;</span>  <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;testField&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;abc&#39;</span><span class="p">]</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">index</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 更新一个文档
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">updateTextIndex</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_index&#39;</span><span class="p">,</span>

            <span class="s1">&#39;type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;my_type&#39;</span><span class="p">,</span>

            <span class="s1">&#39;id&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;my_id&#39;</span><span class="p">,</span>

            <span class="s1">&#39;body&#39;</span>  <span class="o">=&gt;</span> <span class="p">[</span>

                <span class="s1">&#39;doc&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>

                    <span class="s1">&#39;new_field&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;abc&#39;</span>

                <span class="p">]</span>

            <span class="p">]</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">update</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 删除一个文档
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">delTextIndex</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_index&#39;</span><span class="p">,</span>

            <span class="s1">&#39;type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;my_type&#39;</span><span class="p">,</span>

            <span class="s1">&#39;id&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;my_id&#39;</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 获取一个文档
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">getTextIndex</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_index&#39;</span><span class="p">,</span>

            <span class="s1">&#39;type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;my_type&#39;</span><span class="p">,</span>

            <span class="s1">&#39;id&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;my_id&#39;</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 搜索一个文档
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">searchText</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_index&#39;</span><span class="p">,</span>

            <span class="s1">&#39;type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;my_type&#39;</span><span class="p">,</span>

            <span class="s1">&#39;body&#39;</span>  <span class="o">=&gt;</span> <span class="p">[</span>

                <span class="s1">&#39;query&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>

                    <span class="s1">&#39;match&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>

                        <span class="s1">&#39;testField&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;abc&#39;</span>

                    <span class="p">]</span>

                <span class="p">]</span>

            <span class="p">]</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">search</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 删除一个索引
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">delIndex</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_index&#39;</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">indices</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 创建一个索引
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">addIndex</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span>   <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_index&#39;</span><span class="p">,</span>

            <span class="s1">&#39;body&#39;</span>  <span class="o">=&gt;</span> <span class="p">[</span>

                <span class="s1">&#39;settings&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>

                    <span class="s1">&#39;number_of_shards&#39;</span>   <span class="o">=&gt;</span> <span class="mi">2</span><span class="p">,</span>

                    <span class="s1">&#39;number_of_replicas&#39;</span> <span class="o">=&gt;</span> <span class="mi">0</span>

                <span class="p">]</span>

            <span class="p">]</span>

        <span class="p">];</span>

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">indices</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">create</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 批量索引
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">bulkTextIndex</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="mi">100</span><span class="p">;</span> <span class="nv">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>

            <span class="nv">$params</span><span class="s1">&#39;body&#39;</span> <span class="o">=</span> <span class="p">[</span>

                <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>

                    <span class="s1">&#39;_index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;my_index&#39;</span><span class="p">,</span>

                    <span class="s1">&#39;_type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;my_type&#39;</span><span class="p">,</span>

                <span class="p">]</span>

            <span class="p">];</span>

 

            <span class="nv">$params</span><span class="s1">&#39;body&#39;</span> <span class="o">=</span> <span class="p">[</span>

                <span class="s1">&#39;my_field&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;my_value&#39;</span><span class="p">,</span>

                <span class="s1">&#39;second_field&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;some more values&#39;</span>

            <span class="p">];</span>

        <span class="p">}</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">bulk</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 删除一个索引,实际示例
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">delIndexExample</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;news_index&#39;</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">indices</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">delete</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 批量索引,实际示例
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     * @throws \Exception
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">bulkTextIndexExample</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="c1">//请求教育家内网接口
</span><span class="c1"></span>
        <span class="nx">jYJNewsList</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">getJYJNewsList</span><span class="p">();</span>

 

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[];</span>

        <span class="k">foreach</span> <span class="p">(</span><span class="nx">jYJNewsList</span> <span class="k">as</span> <span class="nx">key</span> <span class="o">=&gt;</span> <span class="nv">$news</span><span class="p">)</span> <span class="p">{</span>

            <span class="nv">$params</span><span class="s1">&#39;body&#39;</span> <span class="o">=</span> <span class="p">[</span>

                <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>

                    <span class="s1">&#39;_index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;news_index&#39;</span><span class="p">,</span>

                    <span class="s1">&#39;_type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;news_type&#39;</span><span class="p">,</span>

                    <span class="s1">&#39;id&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;news_id&#39;</span> <span class="o">.</span> <span class="p">(</span><span class="nv">$key</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span>

                <span class="p">]</span>

            <span class="p">];</span>

 

            <span class="nv">$params</span><span class="s1">&#39;body&#39;</span> <span class="o">=</span> <span class="p">[</span>

                <span class="s1">&#39;id&#39;</span>         <span class="o">=&gt;</span> <span class="nv">$key</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>

                <span class="s1">&#39;news_id&#39;</span>    <span class="o">=&gt;</span> <span class="nv">$news</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">],</span>

                <span class="s1">&#39;news_title&#39;</span> <span class="o">=&gt;</span> <span class="nv">$news</span><span class="p">[</span><span class="s1">&#39;text&#39;</span><span class="p">],</span>

            <span class="p">];</span>

        <span class="p">}</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">bulk</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 获取教育家资讯文数据，调用内网教育家接口
</span><span class="sd">
</span><span class="sd">     * @param array $ids
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     * @throws \Exception
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">protected</span> <span class="k">function</span> <span class="nf">getJYJNewsList</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="c1">//请求内网接口
</span><span class="c1"></span>
        <span class="nv">$qs</span>           <span class="o">=</span> <span class="p">[</span><span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">19</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">31</span><span class="p">];</span>

        <span class="nx">educationUrl</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">getConfigCollegeInfo</span><span class="p">(</span><span class="s1">&#39;education_url&#39;</span><span class="p">);</span>

        <span class="nv">$newsList</span>     <span class="o">=</span> <span class="p">[];</span>

        <span class="k">foreach</span> <span class="p">(</span><span class="nx">qs</span> <span class="k">as</span> <span class="nx">a</span><span class="p">)</span> <span class="p">{</span>

            <span class="nx">url</span>      <span class="o">=</span> <span class="nx">educationUrl</span> <span class="o">.</span> <span class="s1">&#39;/api/v2/news/source/news_csearch_yyf?q=&#39;</span> <span class="o">.</span> <span class="nv">$a</span><span class="p">;</span>

            <span class="nx">data</span>     <span class="o">=</span> <span class="nx">http_get</span><span class="p">(</span><span class="nx">url</span><span class="p">);</span>

            <span class="nx">data</span>     <span class="o">=</span> <span class="nx">data</span> <span class="o">?</span> <span class="nx">json_decode</span><span class="p">(</span><span class="nv">$data</span><span class="p">,</span> <span class="k">true</span><span class="p">)</span> <span class="o">:</span> <span class="p">[];</span>

            <span class="nx">newsList</span> <span class="o">=</span> <span class="nx">array_merge</span><span class="p">(</span><span class="nx">newsList</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>

        <span class="p">}</span>

 

        <span class="k">return</span> <span class="nv">$newsList</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 获取一个文档,实际示例
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">getTextIndexExample</span><span class="p">()</span>

    <span class="p">{</span>

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;news_index&#39;</span><span class="p">,</span>

            <span class="s1">&#39;type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;news_type&#39;</span><span class="p">,</span>

            <span class="s1">&#39;id&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;news_id_100&#39;</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="k">return</span> <span class="nv">$response</span><span class="p">;</span>

    <span class="p">}</span>

 

    <span class="sd">/**
</span><span class="sd">
</span><span class="sd">     * 搜索一个文档,实际示例
</span><span class="sd">
</span><span class="sd">     * @return array
</span><span class="sd">
</span><span class="sd">     */</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">searchTextExample</span><span class="p">(</span><span class="nx">keywords</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="nx">page</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="nv">$pageSize</span> <span class="o">=</span> <span class="mi">10</span><span class="p">)</span>

    <span class="p">{</span>

        <span class="c1">//查询条件
</span><span class="c1"></span>
        <span class="nv">$query</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s2">&#34;match_all&#34;</span> <span class="o">=&gt;</span> <span class="k">new</span> <span class="nx">\stdClass</span><span class="p">()</span>

        <span class="p">];</span>

        <span class="k">if</span> <span class="p">(</span><span class="nv">$keywords</span><span class="p">)</span> <span class="p">{</span>

            <span class="nv">$query</span> <span class="o">=</span> <span class="p">[</span>

                <span class="s1">&#39;match&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>

                    <span class="s1">&#39;news_title&#39;</span> <span class="o">=&gt;</span> <span class="nv">$keywords</span>

                <span class="p">]</span>

            <span class="p">];</span>

        <span class="p">}</span>

 

        <span class="nv">$params</span> <span class="o">=</span> <span class="p">[</span>

            <span class="s1">&#39;index&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;news_index&#39;</span><span class="p">,</span>

            <span class="s1">&#39;type&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;news_type&#39;</span><span class="p">,</span>

            <span class="s1">&#39;body&#39;</span>  <span class="o">=&gt;</span> <span class="p">[</span>

                <span class="s1">&#39;query&#39;</span> <span class="o">=&gt;</span> <span class="nv">$query</span><span class="p">,</span>

                <span class="s1">&#39;from&#39;</span>  <span class="o">=&gt;</span> <span class="p">(</span><span class="nx">page</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="nx">pageSize</span><span class="p">,</span>  <span class="c1">// 分页
</span><span class="c1"></span>
                <span class="s1">&#39;size&#39;</span>  <span class="o">=&gt;</span> <span class="nv">$pageSize</span><span class="p">,</span>  <span class="c1">// 每页数量
</span><span class="c1"></span>
                <span class="s1">&#39;sort&#39;</span>  <span class="o">=&gt;</span> <span class="p">[</span>  <span class="c1">// 排序
</span><span class="c1"></span>
                    <span class="s1">&#39;id&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;asc&#39;</span>   <span class="c1">//对age字段进行降序排序
</span><span class="c1"></span>
                <span class="p">]</span>

            <span class="p">]</span>

        <span class="p">];</span>

 

        <span class="nx">response</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="na">client</span><span class="o">-&gt;</span><span class="na">search</span><span class="p">(</span><span class="nv">$params</span><span class="p">);</span>

 

        <span class="nv">$list</span> <span class="o">=</span> <span class="p">[];</span>

        <span class="k">foreach</span> <span class="p">(</span><span class="nx">response</span><span class="p">[</span><span class="s1">&#39;hits&#39;</span><span class="p">][</span><span class="s1">&#39;hits&#39;</span><span class="p">]</span> <span class="k">as</span> <span class="nx">hit</span><span class="p">)</span> <span class="p">{</span>

            <span class="k">list</span><span class="p">[]</span> <span class="o">=</span> <span class="nx">hit</span><span class="p">[</span><span class="s1">&#39;_source&#39;</span><span class="p">];</span>

        <span class="p">}</span>

 

        <span class="k">return</span> <span class="p">[</span>

            <span class="s1">&#39;total&#39;</span>      <span class="o">=&gt;</span> <span class="p">(</span><span class="nx">int</span><span class="p">)</span><span class="nv">$response</span><span class="s1">&#39;hits&#39;</span><span class="p">,</span>

            <span class="s1">&#39;page&#39;</span>       <span class="o">=&gt;</span> <span class="p">(</span><span class="nx">int</span><span class="p">)</span><span class="nv">$page</span><span class="p">,</span>

            <span class="s1">&#39;page_size&#39;</span>  <span class="o">=&gt;</span> <span class="p">(</span><span class="nx">int</span><span class="p">)</span><span class="nv">$pageSize</span><span class="p">,</span>

            <span class="s1">&#39;page_total&#39;</span> <span class="o">=&gt;</span> <span class="nx">ceil</span><span class="p">(</span><span class="nx">response</span><span class="p">[</span><span class="s1">&#39;hits&#39;</span><span class="p">][</span><span class="s1">&#39;total&#39;</span><span class="p">]</span> <span class="o">/</span> <span class="nx">pageSize</span><span class="p">),</span>

            <span class="s1">&#39;list&#39;</span>       <span class="o">=&gt;</span> <span class="nv">$list</span>

        <span class="p">];</span>

    <span class="p">}</span>

<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="3保持-elasticsearch数据最新"><strong>3.保持</strong> <strong>Elasticsearch数据最新</strong></h3>
<h4 id="a工具同步httpsmyoschinanetu4000872blog2252620">A、工具同步（<a href="https://my.oschina.net/u/4000872/blog/2252620">https://my.oschina.net/u/4000872/blog/2252620</a>）</h4>
<h5 id="1-logstash-input-jdbc"><strong>1). logstash-input-jdbc</strong></h5>
<p>logstash官方插件,集成在logstash中,下载logstash即可,通过配置文件实现mysql与elasticsearch数据同步</p>
<p>优点</p>
<p>能实现mysql数据全量和增量的数据同步,且能实现定时同步.</p>
<p>版本更新迭代快,相对稳定.</p>
<p>作为ES固有插件logstash一部分,易用</p>
<p>缺点</p>
<p>不能实现同步删除操作,MySQL数据删除后Elasticsearch中数据仍存在.</p>
<p>同步最短时间差为一分钟,一分钟数据同步一次,无法做到实时同步.</p>
<h5 id="2-go-mysql-elasticsearch"><strong>2). go-mysql-elasticsearch</strong></h5>
<p>go-mysql-elasticsearch 是国内作者开发的一款插件</p>
<p>优点</p>
<p>能实现mysql数据增加,删除,修改操作的实时数据同步</p>
<p>缺点</p>
<p>无法实现数据全量同步Elasticsearch</p>
<p>仍处理开发、相对不稳定阶段</p>
<h5 id="3-elasticsearch-jdbc"><strong>3). elasticsearch-jdbc</strong></h5>
<p>目前最新的版本是2.3.4，支持的ElasticSearch的版本为2.3.4, 未实践</p>
<p>优点</p>
<p>能实现mysql数据全量和增量的数据同步.</p>
<p>缺点</p>
<p>目前最新的版本是2.3.4，支持的ElasticSearch的版本为2.3.4</p>
<p>不能实现同步删除操作,MySQL数据删除后Elasticsearch中数据仍存在</p>
<h4 id="b使用消息队列实现es增量同步httpswwwjianshucomp2c968d23ef85">B、使用消息队列实现ES增量同步（<a href="https://www.jianshu.com/p/2c968d23ef85">https://www.jianshu.com/p/2c968d23ef85</a>）</h4>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">ZhangKQ</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2022-02-10
        
    </span>
  </p>
  
  
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/wechat-qr-code.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/qrcode/alipay-qr-code.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/%E4%B8%AD%E9%97%B4%E4%BB%B6/">中间件</a>
          <a href="/tags/middleware/">middleware</a>
          <a href="/tags/elasticsearch/">elasticsearch</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/middleware/elasticsearch%E5%9C%A8docker%E5%AE%89%E8%A3%85%E5%8F%8A%E4%BD%BF%E7%94%A8/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">[elasticsearch]Elasticsearch在docker安装及使用</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/middleware/elasticsearch%E5%8E%9F%E7%90%86%E5%92%8C%E8%AF%AD%E6%B3%95/">
            <span class="next-text nav-default">[elasticsearch]Elasticsearch原理和语法</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wdyxzkq@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/dysoso" class="iconfont icon-github" title="github"></a>
      <a href="https://gitee.com/dysoso" class="iconfont icon-gitlab" title="gitlab"></a>
  <a href="https://blog.nevergiveup.tech/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://blog.nevergiveup.tech/">blog.nevergiveup.tech</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy; 
    2021 - 
    2022<span class="heart"><i class="iconfont icon-heart"></i></span><span><a href="https://beian.miit.gov.cn/">蜀ICP备2021005948号-1</a></span>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>


<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
	ga('create', 'never-give-up', 'auto');
	ga('set', 'anonymizeIp', true);
	ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>







</body>
</html>
